package com.itheima.dao.impl;

import com.itheima.dao.IStudentDao;
import com.itheima.domain.Student;

import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {
    private static ArrayList<Student> list= new ArrayList<>();
    static {
        Student student1 = new Student("0001", "张三", 23, "9999");
        Student student2 = new Student("0002", "李四", 24, "8888");
        Student student3 = new Student("0003", "王五", 25, "7777");
        Student student4 = new Student("0004", "赵六", 26, "6666");
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);

    }
    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        list.remove(index);
    }

    @Override
    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
        list.set(index,newStu);
    }

    @Override
    public ArrayList<Student> findAllStudents() {
        return list;
    }

    @Override
    public int getIndex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }
}
